package com.sky.service.impl;

import com.sky.dto.OrdersOverviewDTO;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 获取指定时间的运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDate begin, LocalDate end) {

        // 获取新增用户数量
        Map map = new HashMap();
        map.put("begin",LocalDateTime.of(begin, LocalTime.MIN));
        map.put("end",LocalDateTime.of(end,LocalTime.MAX));
        Integer newUsers = userMapper.countByMap(map);

        // 获取今日营业额
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumByMap(map);
        // 如果今日没有营业额，则会返回null
        turnover = turnover == null ? 0 : turnover;

        // 获取今日完成订单的订单数量(有效订单数)
        Integer validOrderCount = orderMapper.countByMap(map);

        // 获取今日的总订单数量(包括取消的订单，派送中的订单)
        map.remove("status");
        Integer allOrderCount = orderMapper.countByMap(map);

        return BusinessDataVO
                .builder()
                .newUsers(newUsers)
                .orderCompletionRate(allOrderCount != 0 ? validOrderCount * 1.0 / allOrderCount : 0.0)
                .turnover(turnover)
                .unitPrice(validOrderCount != 0 ? turnover * 1.0 / validOrderCount : 0.0)
                .validOrderCount(validOrderCount)
                .build();
    }


    /**
     * 获取今日菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getTodayOverviewDishes() {
        // 停售套餐数量
        Map map = new HashMap();
        map.put("status",0);
        Integer discontinued = dishMapper.countByMap(map);
        // 启售套餐数量
        map.put("status",1);
        Integer sold = dishMapper.countByMap(map);
        return DishOverViewVO
                .builder()
                .discontinued(discontinued) // 停售菜品数量
                .sold(sold) // 启售菜品数量
                .build();
    }

    /**
     * 获取今日套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getTodayOverviewSetmeals() {
        // 停售套餐数量
        Map map = new HashMap();
        map.put("status",0);
        Integer discontinued = setmealMapper.countByMap(map);
        // 启售套餐数量
        map.put("status",1);
        Integer sold = setmealMapper.countByMap(map);
        return SetmealOverViewVO
                .builder()
                .discontinued(discontinued) // 停售菜品数量
                .sold(sold) // 启售菜品数量
                .build();
    }


    /**
     * 查询今日订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getTodayOrderView() {
        Map map = new HashMap();
        map.put("begin",LocalDateTime.of(LocalDate.now(),LocalTime.MIN));
        map.put("end",LocalDateTime.of(LocalDate.now(),LocalTime.MAX));
        List<OrdersOverviewDTO> ordersOverviewDTOS
                = orderMapper.countStatusByMap(map);
        Integer allOrders = 0;
        Integer cancelledOrders = 0;
        Integer completedOrders = 0;
        Integer deliveredOrders = 0;
        Integer waitingOrders = 0;
        for (OrdersOverviewDTO ordersOverviewDTO : ordersOverviewDTOS) {
            Integer status = ordersOverviewDTO.getStatus();
            Integer count = ordersOverviewDTO.getCount();
            if (status == Orders.TO_BE_CONFIRMED) {
                waitingOrders += count;
            }else if (status == Orders.CANCELLED) {
                cancelledOrders += count;
            }else if (status == Orders.COMPLETED) {
                completedOrders += count;
            }else if (status == Orders.CONFIRMED) {
                deliveredOrders += count;
            }
            allOrders += count;
        }

        return OrderOverViewVO
                .builder()
                .allOrders(allOrders) // 全部订单
                .cancelledOrders(cancelledOrders) // 已取消的订单
                .completedOrders(completedOrders) // 已完成数量
                .deliveredOrders(deliveredOrders) // 待派送数量
                .waitingOrders(waitingOrders) // 待接单数量
                .build();
    }
}
